import org.junit.Test;

import java.io.*;
import java.util.Scanner;

public class ByteStreamT {
    public static void main(String[] args) {
        String SourcePath;
        String GoalPath;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入源文件目录");
        SourcePath = sc.nextLine();
        System.out.println("请输入目标目录");
        GoalPath = sc.nextLine();
        sc.close();
        long ST = System.currentTimeMillis();
        CopyI(SourcePath,GoalPath);
        long ET = System.currentTimeMillis();
        System.out.println("totally time is "+(ET-ST));
    }
    @Test
    public void CopyTest()  {
        FileInputStream fr = null;
        FileOutputStream fw = null;
        try {
            File f1 = new File("J:\\JavaTestArea\\JTA2\\测试图例.jpg");
            File f2 = new File("J:\\JavaTestArea\\JTA2\\测试图例2.jpg");
            fr = new FileInputStream(f1);
            fw = new FileOutputStream(f2,true);
            int len ;
            byte [] temp = new byte[1024];
            while ((len=fr.read(temp))!=-1){
                fw.write(temp,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr!=null)
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fw!=null)
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void CopyI(String SourcePath,String GoalPath){
        FileInputStream fr = null;
        FileOutputStream fw = null;
        try {
            File f1 = new File(SourcePath);
            File f2 = new File(GoalPath);
            fr = new FileInputStream(f1);
            fw = new FileOutputStream(f2,true);
            int len ;
            byte [] temp = new byte[1048576];
            //这里分配更大内存时速度就会更快
            while ((len=fr.read(temp))!=-1){
                fw.write(temp,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fr!=null)
                    fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fw!=null)
                    fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//I:\gumu\033006\Darklust Collection\ULTIMATE-END 12.mp4
        //J:\JavaTestArea\lpl.mp4
    }
}
